package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.OrderSettingDto;
import com.itheima.health.service.OrderSettingService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author spt
 * @description 预约设置控制器
 * @date
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "后台预约设置接口")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    //任务四：上传文件批量添加
    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile)  {
        //读取execl文件
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(excelFile.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //读取第一张sheet表
        XSSFSheet sheet = workbook.getSheetAt(0);

        //遍历每一个单元格，将一行数据装入一个String类型的集合中，再将数组装入集合中
        List<ArrayList<String>> sheetData = new ArrayList<>();
        //循环每一行 getPhysicalNumberOfRows获取不为空的行个数
        for (int rowIndex = 1; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
            Row row = sheet.getRow(rowIndex);

            //用来存放每一行的数据
            ArrayList<String> rowData = new ArrayList<>();
            //循环每一列 getPhysicalNumberOfCells获取不为空的列个数
            for (int cellIndex = 0; cellIndex < row.getPhysicalNumberOfCells(); cellIndex++) {
                //得到每一个单元格
                Cell cell = row.getCell(cellIndex);

                //处理日期
                switch (cellIndex) {
                    case 0:
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                        rowData.add(sdf.format(cell.getDateCellValue()));
                        break;
                    case 1:
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        rowData.add(cell.getStringCellValue());
                        break;
                }
            }
            sheetData.add(rowData);
        }

        System.out.println(sheetData);
        orderSettingService.addBatch(sheetData);
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }





    //设置每日可预约的人数
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){

        log.info("预约人数设置:{}",orderSetting);
        orderSettingService.editNumber(orderSetting);
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }





    @GetMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(int year, int month) {
        log.info("查询年份:{}月份:{}", year, month);
        List<Map<String, Object>> list = orderSettingService.getOrderSettingByMonth(year, month);
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, list);

}}

